package edu.uoc.tdp.pac4.client.logandreg;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import edu.uoc.tdp.pac4.beans.AttendanceStudent;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;
import javax.swing.JLabel;
import javax.swing.table.TableModel;



public class Attendance_CheckOutAttendances extends JPanel {
	
	
	private List<AttendanceData> onTimeAttendances = null;
	private static final long serialVersionUID = 1L;
	private JPanel resultsPanel = null;
	private JScrollPane resultsScroll = null;
	private JButton btnSearch = null;
	private JButton btnMark = null;
	private JTable table = null;
	ClientConnectionHelper connectionHelper_ = null;
	


	public Attendance_CheckOutAttendances(ClientConnectionHelper connectionHelper) {
		setLayout(new BorderLayout(0, 0));

		connectionHelper_ = connectionHelper;
		resultsPanel = new JPanel();

		add(resultsPanel, BorderLayout.CENTER);
		resultsPanel.setLayout(new BorderLayout());
		
		resultsScroll = new JScrollPane();
		resultsPanel.add(resultsScroll, BorderLayout.CENTER);

		table = getAttendancesTable();
		resultsScroll.add(table);
		resultsScroll.setViewportView(table);
		
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.SOUTH);
		btnSearch = new JButton(LanguageUtils.getMessage("client.button.search"));
		btnSearch.setHorizontalAlignment(SwingConstants.RIGHT);
		toolBar.add(btnSearch);	
		
		JLabel label = new JLabel("                ");
		toolBar.add(label);
		btnMark = new JButton(LanguageUtils.getMessage("client.regandlog.button.mark"));
		btnMark.setHorizontalAlignment(SwingConstants.RIGHT);
		toolBar.add(btnMark);
		
		btnSearch.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						if (e.getSource() == btnSearch) {
							try {
								onTimeAttendances = new AttDataHelper().getAttendaces(connectionHelper_);
								table = getAttendancesTable();
								resultsScroll.setViewportView(table);
							}catch(Exception e2) {
									JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
											LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
							}
						
					}
					}
					});
		
		btnMark.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						if (e.getSource() == btnMark) {
							try{
								table.getSelectedRow();							
								int i = table.getSelectedRow();
								TableModel model2 = table.getModel();
								int identificador = (Integer) model2.getValueAt(i,3);
								AttendanceStudent attStd = new AttendanceStudent(identificador,ClientConnectionHelper.getLoggedUser().getNIF());
								if(connectionHelper_.getLogAndRegManager().checkInAssistanceStudent(attStd)){
									JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.regandlog.info.attcheckedin"),
											LanguageUtils.getMessage("client.regandlog.info.title.attcheckedin"),JOptionPane.INFORMATION_MESSAGE);
								}	
								else{
									JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.regandlog.warning.attcheckin"),
											LanguageUtils.getMessage("client.regandlog.warning.title.attcheckin"),JOptionPane.WARNING_MESSAGE);
								}
									
							}catch(Exception e2){
								JOptionPane.showMessageDialog(null, LanguageUtils.getMessage("client.error.message"),
										LanguageUtils.getMessage("client.error.title"),JOptionPane.ERROR_MESSAGE);
							}
						}
					}
				}
		);
	}
	
	private JTable getAttendancesTable() {

		JTable tableStudentResults = null;
		if (tableStudentResults == null) {
			tableStudentResults = new JTable();
			OnTimeAttendancesModel model = new OnTimeAttendancesModel(onTimeAttendances);
			tableStudentResults.setModel(model);
		}
		tableStudentResults.setEnabled(true);
		return tableStudentResults;
	}	

}
